﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace AuthenticationSection
{
    public class HttpCustomResponse : ICustomResponse
    {
        private static HttpCustomResponse _HttpCustomResponse = null;

        public static HttpCustomResponse GetInstance
        {
            get
            {
                if (_HttpCustomResponse == null)
                    _HttpCustomResponse = new HttpCustomResponse();
                return _HttpCustomResponse;
            }
        }

        public HttpResponse response { get; set; }

        public HttpRequest request { get; set; }

        public void SaveBillsInCookie(string bills)
        {
            HttpCookie cookie = new HttpCookie(Unit.cookieName, bills);
            cookie.Expires = DateTime.UtcNow.AddMinutes(30);
            response.Cookies.Add(cookie);
        }

        public string GetBills()
        {
            var strCookie = request.Cookies[Unit.cookieName];
            if (strCookie != null && strCookie.Value != null)
            {
                BillsModel model = Authentication.DecryptionBill(strCookie.Value);
                if (DateTime.Compare(model.ExpirationDate.AddMinutes(30), model.IssueDate) < 1)
                {
                    return strCookie.Value;
                }
            }
            return "";
        }

        public void SetBills(string bills)
        {
            var strCookie = request.Cookies[Unit.cookieName];
            if (strCookie != null && strCookie.Value != null)
            {
                strCookie.Value = bills;
            }
        }

        public void RemoveBills()
        {
            HttpCookie cookie = request.Cookies[Unit.cookieName];
            if (cookie != null && cookie.Value != null)
            {
                HttpCookie ck = new HttpCookie(Unit.cookieName);
                ck.Expires = DateTime.UtcNow.AddDays(-1d);
                response.Cookies.Add(ck);
            }
        }
    }
}
